﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmTheDocGia : PRE.Catalog.FrmBase
    {
       

        #region Variable
        DAL.EntitiesDAL.TheDocGiaDAL _dal = new DAL.EntitiesDAL.TheDocGiaDAL();
        DAL.EntitiesDAL.LoaiDocGiaDAL _dal2 = new DAL.EntitiesDAL.LoaiDocGiaDAL();
        BLL.EntitiesBLL.TheDocGiaBLL _bll = new BLL.EntitiesBLL.TheDocGiaBLL();

        DataTable _dt = new DataTable();
        DataTable _dt2 = new DataTable();
        #endregion

        #region Inits
        public FrmTheDocGia()
        {
            InitializeComponent();
        }

        #region Form Load

        private void FrmDauSach_Load(object sender, EventArgs e)
        {
            PerformRefresh();


        }


        #endregion
        #endregion

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txt_MaDocGia.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);

                    cbo_MaLoaiDocGia.ResetText();
                    cbo_ChonHinh.Visible = true;
                    labe_DoiTuong.Visible = false;
                    txt_DoiTuong.Visible = false;

                    txt_TenFile.Visible = false;
                    txt_MaDocGia.Properties.ReadOnly = true;

                    dte_NgayBatDau.EditValue = DateTime.Now;
                    dte_NgayHetHan.EditValue = DateTime.Now.AddYears(1);
                    dte_NgayBatDau.Properties.ReadOnly = true;
                    dte_NgayHetHan.Properties.ReadOnly = true;


                }

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = gridView_TheDocGia.GetFocusedRowCellValue("MADOCGIA") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txt_MaDocGia.Properties.ReadOnly = true;
            cbo_ChonHinh.Visible = true;
            txt_TenFile.Visible = false;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            Catalog.FrmBase x = new Catalog.FrmBase();
            string _NgayKy = "TP.Cần Thơ, ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
            DateTime _NgaySinh = Convert.ToDateTime(dte_NgaySinh.EditValue);
            DateTime _Ngaybatdau = Convert.ToDateTime(dte_NgayBatDau.EditValue);
            DateTime _Ngayhethan = Convert.ToDateTime(dte_NgayHetHan.EditValue);

            Reports.FrmReportView frm = new Reports.FrmReportView();
            frm._load_XtraTheDocGia(_dt, txt_MaDocGia.Text, x.Proper(txt_HoVaTen.Text), _NgaySinh.ToString("dd/MM/yyyy")
                , x.Proper(txt_NgheNghiep.Text), x.Proper(txt_DiaChi.Text), _Ngaybatdau.ToString("dd-MM-yyyy"), _Ngayhethan.ToString("dd-MM-yyyy"), _NgayKy.ToString());
            frm.Show();

            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txt_DoiTuong.Properties.ReadOnly = true;
            ResetText();
            cbo_ChonHinh.Visible = false;
            btnGoSearch.Visible = false;
            txt_TenFile.Visible = false;
            labe_DoiTuong.Visible = true;
            txt_DoiTuong.Visible = true;

            if (_dt != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dt.Rows.Count > 0) DataBindingControl();
            }



            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {

            var name_HoTen = Proper(txt_HoVaTen.Text);
            var name_DiaChi = Proper(txt_DiaChi.Text);
            var name_NgheNghiep = Proper(txt_NgheNghiep.Text);
            var name_NoiCongTac = Proper(txt_NoiCongTac.Text);
            var tenFileAnh = txt_TenFile.Text.Trim();

            if (IsAdd)
            {
                
                var o = new DAL.Entities.TheDocGia()
                {
                    MADOCGIA= txt_MaDocGia.Text,
                    HODOCGIA = name_HoTen,
                    NGAYSINH = Convert.ToDateTime(dte_NgaySinh.Text),
                    GIOITINH = Convert.ToBoolean(radio_Gioitinh.EditValue),
                    SCMND = txt_SoCMND.Text,
                    DIACHI = name_DiaChi,
                    DIENTHOAI = txt_SoDienThoai.Text,
                    NGHENGHIEP = name_NgheNghiep,
                    NOICONGTAC = name_NoiCongTac,
                    NGAYBATDAU = Convert.ToDateTime(dte_NgayBatDau.Text),
                    NGAYHETHAN = Convert.ToDateTime(dte_NgayHetHan.Text),              
                    MALOAIDG = Convert.ToString(cbo_MaLoaiDocGia.EditValue),
                    IMAGES = txt_TenFile.Text
                  
                 
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                   // PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = gridView_TheDocGia.GetFocusedRowCellValue("MADOCGIA") + "";
               
                var o = new DAL.Entities.TheDocGia()
                {
                    MADOCGIA = txt_MaDocGia.Text,
                    HODOCGIA = name_HoTen,
                    GIOITINH = Convert.ToBoolean(radio_Gioitinh.EditValue),
                    SCMND = txt_SoCMND.Text,
                    DIACHI = name_DiaChi,
                    DIENTHOAI = txt_SoDienThoai.Text,
                    NGHENGHIEP = name_NgheNghiep,
                    NOICONGTAC = name_NoiCongTac,
                    MALOAIDG = Convert.ToString(cbo_MaLoaiDocGia.EditValue),
                    IMAGES = txt_TenFile.Text
                  
                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_MaDocGia.ResetText();
            txt_HoVaTen.ResetText();
            dte_NgaySinh.ResetText();
            radio_Gioitinh.ResetText();
            txt_SoCMND.ResetText();
            txt_DiaChi.ResetText();
            txt_SoDienThoai.ResetText();
            txt_NgheNghiep.ResetText();
            txt_NoiCongTac.ResetText();
            dte_NgayBatDau.ResetText();
            dte_NgayHetHan.ResetText();
            cbo_MaLoaiDocGia.ResetText();
            txt_DoiTuong.ResetText();
            txt_TenFile.ResetText();

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_MaDocGia.DataBindings.Clear();
            txt_HoVaTen.DataBindings.Clear();
            dte_NgaySinh.DataBindings.Clear();
            radio_Gioitinh.DataBindings.Clear();
            txt_SoCMND.DataBindings.Clear();
            txt_DiaChi.DataBindings.Clear();
            txt_SoDienThoai.DataBindings.Clear();
            txt_NgheNghiep.DataBindings.Clear();
            txt_NoiCongTac.DataBindings.Clear();
            dte_NgayBatDau.DataBindings.Clear();
            dte_NgayHetHan.DataBindings.Clear();
            cbo_MaLoaiDocGia.DataBindings.Clear();
            txt_DoiTuong.DataBindings.Clear();
            txt_TenFile.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_MaDocGia.DataBindings.Add("Text", _dt, ".MADOCGIA");
            txt_HoVaTen.DataBindings.Add("Text", _dt, ".HODOCGIA");
            dte_NgaySinh.DataBindings.Add("EditValue", _dt, ".NGAYSINH");
            radio_Gioitinh.DataBindings.Add("EditValue", _dt, ".GIOITINH");
            txt_SoCMND.DataBindings.Add("Text", _dt, ".SCMND");
            txt_DiaChi.DataBindings.Add("Text", _dt, ".DIACHI");
            txt_SoDienThoai.DataBindings.Add("Text", _dt, ".DIENTHOAI");
            txt_NgheNghiep.DataBindings.Add("Text", _dt, ".NGHENGHIEP");
            txt_NoiCongTac.DataBindings.Add("Text", _dt, ".NOICONGTAC");
            dte_NgayBatDau.DataBindings.Add("EditValue", _dt, ".NGAYBATDAU");
            dte_NgayHetHan.DataBindings.Add("EditValue", _dt, ".NGAYHETHAN");
            cbo_MaLoaiDocGia.DataBindings.Add("EditValue", _dt, ".MALOAIDG");
            txt_DoiTuong.DataBindings.Add("Text", _dt, ".DOITUONG");
            txt_TenFile.DataBindings.Add("EditValue", _dt, ".IMAGES");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txt_MaDocGia.Properties.ReadOnly = isReadOnly;
            txt_HoVaTen.Properties.ReadOnly = isReadOnly;
            dte_NgaySinh.Properties.ReadOnly = isReadOnly;
            radio_Gioitinh.Properties.ReadOnly = isReadOnly;
            txt_SoCMND.Properties.ReadOnly = isReadOnly;
            txt_DiaChi.Properties.ReadOnly = isReadOnly;
            txt_SoDienThoai.Properties.ReadOnly = isReadOnly;
            txt_NgheNghiep.Properties.ReadOnly = isReadOnly;
            txt_NoiCongTac.Properties.ReadOnly = isReadOnly;
            dte_NgayBatDau.Properties.ReadOnly = isReadOnly;
            dte_NgayHetHan.Properties.ReadOnly = isReadOnly;
            cbo_MaLoaiDocGia.Properties.ReadOnly = isReadOnly;

            gridControl_TheDocGia.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            ResetText();

            txt_TenFile.Visible = false;
            btnGoSearch.Visible = true;

            gridControl_TheDocGia.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGoSearch_Click_1(object sender, EventArgs e)
        {
            var name_HoTen = Proper(txt_HoVaTen.Text);
            var name_SoCMND = Proper(txt_SoCMND.Text);
            var name_DiaChi = Proper(txt_DiaChi.Text);
            var name_DienThoai = Proper(txt_SoDienThoai.Text);
            var name_NgheNghiep = Proper(txt_NgheNghiep.Text);
            var name_NoiCongTac = Proper(txt_NoiCongTac.Text);

            var o = new DAL.Entities.TheDocGia()
            {
                MADOCGIA = Convert.ToString(txt_MaDocGia.Text),
                HODOCGIA = name_HoTen,
                SCMND = name_SoCMND,
                DIACHI = name_DiaChi,
                DIENTHOAI = name_DienThoai,
                NGHENGHIEP = name_NgheNghiep,
                NOICONGTAC = name_NoiCongTac,
                MALOAIDG = Convert.ToString(cbo_MaLoaiDocGia.EditValue)
            };

            gridControl_TheDocGia.DataSource = _dal.Search(o);

        }
       
        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dt = _dal.Select();
            _dt2 = _dal2.Select();
            txt_TenFile.Visible = false;
            cbo_MaLoaiDocGia.Properties.DataSource = _dt2;
           
            try
            {
                if (_dt != null)
                {
                    gridControl_TheDocGia.DataSource = _dt;

                    try
                    {
                        string fileName = gridView_TheDocGia.GetFocusedDataRow()["IMAGES"].ToString();
                        pic_Hinh.Image = Image.FromFile(fileName);
                        pic_Hinh.SizeMode = PictureBoxSizeMode.StretchImage;
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show("Không tìm thấy đường dẫn hình ảnh" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    AppGridView.SummaryField(gridView_TheDocGia, "MADOCGIA", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);

                    gridView_TheDocGia.Columns["IMAGES"].Visible = false;
                    gridView_TheDocGia.OptionsBehavior.ReadOnly = true;
                    gridView_TheDocGia.OptionsView.ColumnAutoWidth = true;
                }

                base.LoadData();
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog FileAnh = new OpenFileDialog();
            FileAnh.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            FileAnh.ShowDialog();

            txt_TenFile.Text = FileAnh.FileName;
        }

        private void gridView_TheDocGia_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                string fileName = gridView_TheDocGia.GetFocusedDataRow()["IMAGES"].ToString();
                pic_Hinh.Image = Image.FromFile(fileName);
                pic_Hinh.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Không tìm thấy đường dẫn hình ảnh" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
